# ==========================================================
# CONDITIONAL MEDIATION MODEL (UPDATED VISUAL)
# ==========================================================

library(ggplot2)
library(patchwork)

# Ortak görsel parametreler
w <- 0.50; h <- 0.30
gap <- 0.06
cap_off <- 0.18

# ==========================================================
# ÜST PANEL – KAVRAMSAL MODEL
# ==========================================================
nodes_top <- data.frame(
  id = c("X", "M", "Y", "W"),
  x  = c(0.1, 3.1, 6, 3.1),
  y  = c(0.0, 1.0, 0.0, 2.0),
  label = c("italic(X)", "italic(M)", "italic(Y)", "italic(W)"),
  caption = c("Loneliness", "Frailty", "Quality of Life", "Country")
)

p_top <- ggplot() +
  geom_rect(data = nodes_top, aes(xmin = x - w, xmax = x + w, ymin = y - h, ymax = y + h),
            fill = "grey92", color = "black", linewidth = 0.9) +
  geom_text(data = nodes_top, aes(x, y, label = label), parse = TRUE,
            size = 8, family = "Times New Roman") +
  geom_text(data = nodes_top, aes(x, y = y - h - cap_off, label = caption),
            size = 5, family = "Times New Roman") +
  
  # Yollar: X→M, M→Y, X→Y
  geom_segment(aes(x = 0.1 + w + gap, y = 0, xend = 6 - w - gap, yend = 0),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 0.1 + w + gap, y = 0+h, xend = 3.1 - w - gap, yend = 1),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 3.1 + w + gap, y = 1, xend = 6 - w - gap, yend = 0+h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  # Moderatör: W → M ve W → Y (iki ok)
  geom_segment(aes(x = 2.59, y = 2 - h, xend = 1.5, yend = 0.38 + h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 3.61, y = 2 - h, xend = 4.7, yend = 0.38 + h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  annotate("text", x = 3.1, y = 2.7, label = "Conceptual Model",
           family = "Times New Roman", fontface = "bold", size = 6) +
  
  coord_equal(xlim = c(-0.5, 7.5), ylim = c(-0.5, 3.5), expand = TRUE)+
  theme_void()
p_top



  #==========================================================
  # Confounding Kutusu (sağ üst köşe)
  # ==========================================================

# Kutu koordinatları
conf_xmin <- 5.2
conf_xmax <- 6.5
conf_ymin <- 1.8
conf_ymax <- 2.7

p_top <- p_top +
  # İçi boş kutu (sadece kenarlık)
  annotate("rect",
           xmin = conf_xmin, xmax = conf_xmax,
           ymin = conf_ymin, ymax = conf_ymax,
           fill = NA,               # içi boş
           color = "black",         # kenar rengi
           linewidth = 0.7,
           linetype="longdash") +
  
  # Değişken listesi (kutunun içinde ortalanmış)
  annotate("text",
           x = (conf_xmin + conf_xmax) / 2,
           y = conf_ymax - 0.45,
           label = "Sex\nAge\nMarital Status\nEducation\nIncome",
           family = "Times New Roman",
           size = 4.2, lineheight = 1.0) +
  
  # Başlık (kutunun ALTINDA)
  annotate("text",
           x = (conf_xmin + conf_xmax) / 2,
           y = conf_ymin - 0.15,     # kutunun altına konumlandı
           label = "Confounding Variables",
           family = "Times New Roman",
           fontface = "plain",
           size = 5)


# ==========================================================
# Alt PANEL – İSTATİSTİKSEL MODEL
# ==========================================================


nodes_bottom <- data.frame(
  id = c("X", "M", "Y", "W", "XW", "MW"),
  x  = c(0.1, 3.1, 6.0, 0.1, 1.1, 6),
  y  = c(0.0, 2.0, 0.0, 1.2, 2.3, 1.3),
  label = c("italic(X)", "italic(M)", "italic(Y)", "italic(W)", "italic(XW)", "italic(MW)"),
  caption = c("Loneliness", "Frailty", "Quality of Life", "Country", "Interaction (X×W)", "Interaction (M×W)")
)
p_bottom <- ggplot() +
  # Kutular
  geom_rect(data = nodes_bottom,
            aes(xmin = x - w, xmax = x + w, ymin = y - h, ymax = y + h),
            fill = "grey97", color = "black", linewidth = 0.9) +
  
  # Etiketler
  geom_text(data = nodes_bottom, aes(x, y, label = label),
            parse = TRUE, size = 8, family = "Times New Roman") +
  geom_text(data = nodes_bottom,
            aes(x,
                y = ifelse(id %in% c("XW","MW"),
                           y + h + cap_off,     # bu kutuların başlığı üstte
                           y - h - cap_off),    # diğerleri altta
                label = caption),
            size = 5, family = "Times New Roman")+
  
  # Yollar: X → M, M → Y, X → Y
  geom_segment(aes(x = 0.1 + w + gap, y = h,
                   xend = 3.1 - w - gap, yend = 2-h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 3.1 + w + gap, y = 2-h,
                   xend = 6 - w - gap, yend = h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 0.1 + w + gap, y = 0,
                   xend = 6 - w - gap, yend = 0),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  # Moderatör bağlantıları: W → M ve W → Y
  geom_segment(aes(x = 0.1+w, y = 1.0 + h, xend = 3.1 - w, yend = 1.92),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 0.1+w, y = 0.75 + h, xend = 6 - w - gap, yend =0.15),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  # Etkileşim kutularından bağlantılar
  geom_segment(aes(x = 1.1+w, y = 2.3, xend = 3.1-w, yend = 2.1),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 6, y = 1.0, xend = 6, yend = 0 + h+gap),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  # Error term
  geom_segment(aes(x = 6.52, y = 0, xend = 6.52, yend =0),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               color= "grey30") +
  geom_segment(aes(x = 6.9, y = 0, xend = 6.52, yend =0),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               linetype = "longdash",
               color="grey30") +

  geom_segment(aes(x = 3.62, y = 2, xend = 3.62, yend =),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               color="grey30") +
  geom_segment(aes(x = 4, y = 2, xend = 3.62, yend =),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               linetype = "longdash",
               color="grey30") +
  
  # Katsayı etiketleri
  annotate("text", x = 2.2, y = 1.2,
           label = expression(italic(a[11])),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 4.6, y = 1.2,
           label = expression(italic(b[11])),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 3.0, y = -0.2,
           label = expression(italic(c * "'")),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 1.87, y = 1.85,
           label = expression(italic(a[21])),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 2.15, y = 2.3,
           label = expression(italic(a[31])),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 3.8, y = 0.6,
           label = expression(italic(b[21])),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 6.2, y = 0.7,
           label = expression(italic(b[31])),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  ## errors
  annotate("text", x = 4.17, y = 2,
           label = expression(italic(e) * phantom("")[M]),
           size = 5, family = "Times New Roman",
           angle = 0)+

  annotate("text", x = 7.07, y = 0,
           label = expression(italic(e) * phantom("")[Y]),
           size = 5, family = "Times New Roman",
           angle = 0)+
  

  annotate("text", x = 3.1, y = 3.1, label = "Statistical Model",
           family = "Times New Roman", fontface = "bold", size = 6) +
  
  coord_equal(xlim = c(-0.5, 7.5), ylim = c(-0.5, 3.5), expand = TRUE) +
  theme_void()
p_bottom

#
# ==========================================================
# ÜST + ALT PANEL BİRLEŞTİRME
# ==========================================================
library(patchwork)
final_plot <- p_top / p_bottom + plot_layout(heights = c(1, 1))

ggsave("conditional_mediation_full.png", final_plot,
       width = 12, height = 12, dpi = 300)

final_plot












#-------------------------------
# DIRECT EFFECTS BOTH COUNTRY
#----------------------------


# Ortak görsel parametreler
w <- 0.50; h <- 0.30
gap <- 0.06
cap_off <- 0.18

# ==========================================================
# ÜST PANEL – TÜRKİYE
# ==========================================================
nodes_top2 <- data.frame(
  id = c("X", "M", "Y"),
  x  = c(0.1, 3.1, 6),
  y  = c(0.0, 1.0, 0.0),
  label = c("italic(X)", "italic(M)", "italic(Y)"),
  caption = c("Loneliness", "Frailty", "Quality of Life")
)

p_top2 <- ggplot() +
  geom_rect(data = nodes_top2, aes(xmin = x - w, xmax = x + w, ymin = y - h, ymax = y + h),
            fill = "grey92", color = "black", linewidth = 0.9) +
  geom_text(data = nodes_top2, aes(x, y, label = label), parse = TRUE,
            size = 8, family = "Times New Roman") +
  geom_text(data = nodes_top2, aes(x, y = y - h - cap_off, label = caption),
            size = 5, family = "Times New Roman") +
  
  # Yollar: X→M, M→Y, X→Y
  geom_segment(aes(x = 0.1 + w + gap, y = 0, xend = 6 - w - gap, yend = 0),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 0.1 + w + gap, y = 0+h, xend = 3.1 - w - gap, yend = 1),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 3.1 + w + gap, y = 1, xend = 6 - w - gap, yend = 0+h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  annotate("text", x = 3.1, y = -0.2, label = "B= -0.851**",
           size = 5.5, family = "Times New Roman") +
  annotate("text", x = 1.65, y = 0.8, label = "B= 0.208**",
           size = 5.5, family = "Times New Roman", angle=22) +
  annotate("text", x = 4.55, y = 0.8, label = "B= -1.568**",
           size = 5.5, family = "Times New Roman", angle= -22) +
  
  
  annotate("text", x = 3.1, y = 1.7, label = "Türkiye",
           family = "Times New Roman", fontface = "bold", size = 6) +
  
  coord_equal(xlim = c(-0.5, 7.5), ylim = c(-0.6, 2.7), expand = TRUE)+
  theme_void()
p_top2



#==========================================================
# Confounding Kutusu (sağ üst köşe)
# ==========================================================

# Kutu koordinatları
conf_xmin <- 5.2
conf_xmax <- 6.5
conf_ymin <- 1.4
conf_ymax <- 2.3

p_top2 <- p_top2 +
  # İçi boş kutu (sadece kenarlık)
  annotate("rect",
           xmin = conf_xmin, xmax = conf_xmax,
           ymin = conf_ymin, ymax = conf_ymax,
           fill = NA,               # içi boş
           color = "black",         # kenar rengi
           linewidth = 0.7,
           linetype="longdash") +
  
  # Değişken listesi (kutunun içinde ortalanmış)
  annotate("text",
           x = (conf_xmin + conf_xmax) / 2,
           y = conf_ymax - 0.45,
           label = "Sex\nAge\nMarital Status\nEducation\nIncome",
           family = "Times New Roman",
           size = 4.2, lineheight = 1.0) +
  
  # Başlık (kutunun ALTINDA)
  annotate("text",
           x = (conf_xmin + conf_xmax) / 2,
           y = conf_ymin - 0.15,     # kutunun altına konumlandı
           label = "Confounding Variables",
           family = "Times New Roman",
           fontface = "plain",
           size = 5)






# ==========================================================
# ALT PANEL – GERMANY
# ==========================================================
nodes_bottom2 <- data.frame(
  id = c("X", "M", "Y"),
  x  = c(0.1, 3.1, 6),
  y  = c(0.0, 1.0, 0.0),
  label = c("italic(X)", "italic(M)", "italic(Y)"),
  caption = c("Loneliness", "Frailty", "Quality of Life")
)

p_bottom2 <- ggplot() +
  geom_rect(data = nodes_top2, aes(xmin = x - w, xmax = x + w, ymin = y - h, ymax = y + h),
            fill = "grey92", color = "black", linewidth = 0.9) +
  geom_text(data = nodes_top2, aes(x, y, label = label), parse = TRUE,
            size = 8, family = "Times New Roman") +
  geom_text(data = nodes_top2, aes(x, y = y - h - cap_off, label = caption),
            size = 5, family = "Times New Roman") +
  
  # Yollar: X→M, M→Y, X→Y
  geom_segment(aes(x = 0.1 + w + gap, y = 0, xend = 6 - w - gap, yend = 0),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 0.1 + w + gap, y = 0+h, xend = 3.1 - w - gap, yend = 1),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 3.1 + w + gap, y = 1, xend = 6 - w - gap, yend = 0+h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +

  annotate("text", x = 3.1, y = -0.2, label = "B= -1.030**",
           size = 5.5, family = "Times New Roman") +
  annotate("text", x = 1.65, y = 0.8, label = "B= 0.191**",
           size = 5.5, family = "Times New Roman", angle=22) +
  annotate("text", x = 4.55, y = 0.8, label = "B= -1.874**",
           size = 5.5, family = "Times New Roman", angle= -22) +
  
  
  annotate("text", x = 3.1, y = 1.7, label = "Germany",
           family = "Times New Roman", fontface = "bold", size = 6) +
  
  coord_equal(xlim = c(-0.5, 7.5), ylim = c(-0.6, 2.7), expand = TRUE)+
  theme_void()
p_bottom2


final_plot <- p_top2 / p_bottom2 + plot_layout(heights = c(1, 1))

ggsave("directeffect.png", final_plot,
       width = 12, height = 9, dpi = 300)

final_plot















##############
# MODEL
#############

nodes <- data.frame(
  id = c("X", "M", "Y", "W", "XW", "MW"),
  x  = c(0.1, 3.1, 6.0, 0.1, 1.1, 6),
  y  = c(0.0, 2.0, 0.0, 1.2, 2.3, 1.6),
  label = c("italic(X)", "italic(M)", "italic(Y)", "italic(W)", "italic(XW)", "italic(MW)"),
  caption = c("Loneliness", "Frailty", "Quality of Life", "Country", "Interaction (X×W)", "Interaction (M×W)")
)
p_model <- ggplot() +
  # Kutular
  geom_rect(data = nodes,
            aes(xmin = x - w, xmax = x + w, ymin = y - h, ymax = y + h),
            fill = "grey97", color = "black", linewidth = 0.9) +
  
  # Etiketler
  geom_text(data = nodes, aes(x, y, label = label),
            parse = TRUE, size = 8, family = "Times New Roman") +
  geom_text(data = nodes,
            aes(x,
                y = ifelse(id %in% c("XW","MW"),
                           y + h + cap_off,     # bu kutuların başlığı üstte
                           y - h - cap_off),    # diğerleri altta
                label = caption),
            size = 5, family = "Times New Roman")+
  
  # Yollar: X → M, M → Y, X → Y
  geom_segment(aes(x = 0.1 + w + gap, y = h,
                   xend = 3.1 - w - gap, yend = 2-h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 3.1 + w + gap, y = 2-h,
                   xend = 6 - w - gap, yend = h),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  geom_segment(aes(x = 0.1 + w + gap, y = 0,
                   xend = 6 - w - gap, yend = 0),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  # Moderatör bağlantıları: W → M ve W → Y
  geom_segment(aes(x = 0.1+w, y = 1.0 + h, xend = 3.1 - w, yend = 1.92),
               arrow = arrow(length = unit(6, "pt"), type = "open"), linetype = "dashed") +
  geom_segment(aes(x = 0.1+w, y = 0.75 + h, xend = 6 - w - gap, yend =0.15),
               arrow = arrow(length = unit(6, "pt"), type = "open"), linetype = "dashed") +
  
  # Etkileşim kutularından bağlantılar
  geom_segment(aes(x = 1.1+w, y = 2.3, xend = 3.1-w, yend = 2.1),
               arrow = arrow(length = unit(6, "pt"), type = "open"), linetype = "dashed") +
  geom_segment(aes(x = 6, y = 1.3, xend = 6, yend = 0 + h+gap),
               arrow = arrow(length = unit(6, "pt"), type = "open")) +
  
  # Error term
  geom_segment(aes(x = 6.52, y = 0, xend = 6.52, yend =0),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               color= "grey30") +
  geom_segment(aes(x = 6.9, y = 0, xend = 6.52, yend =0),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               linetype = "longdash",
               color="grey30") +
  
  geom_segment(aes(x = 3.62, y = 2, xend = 3.62, yend =),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               color="grey30") +
  geom_segment(aes(x = 4, y = 2, xend = 3.62, yend =),
               arrow = arrow(length = unit(6, "pt"), type = "closed"),
               linetype = "longdash",
               color="grey30") +
  
  # Katsayı etiketleri
  annotate("text", x = 2.2, y = 1.3,
           label = expression(italic(beta == 0.208*"**")),
           size = 5, family = "Times New Roman",
           angle = 38, fontface = "italic") +
  
  annotate("text", x = 4.6, y = 1.1,
           label = expression(italic(beta == -1.46*"**")),
           size = 5, family = "Times New Roman",
           angle = -39, fontface = "italic") +
  
  annotate("text", x = 3.0, y = -0.15,
           label = expression(italic(beta == -0.89*"**")),
           size = 5, family = "Times New Roman",
           angle = 0, fontface = "italic") +
  
  annotate("text", x = 1.87, y = 1.8,
           label = expression(italic(beta == -0.39)),
           size = 5, family = "Times New Roman",
           angle = 15, fontface = "italic") +
  
  annotate("text", x = 2.15, y = 2.3,
           label = expression(italic(beta == 0.002)),
           size = 5, family = "Times New Roman",
           angle = -12, fontface = "italic") +
  
  annotate("text", x = 3.8, y = 0.58,
           label = expression(italic(beta == 2.11)),
           size = 5, family = "Times New Roman",
           angle = -13, fontface = "italic") +
  
  annotate("text", x = 6.1, y = 0.8,
           label = expression(italic(beta == -1.24*"*")),
           size = 5, family = "Times New Roman",
           angle = -90, fontface = "italic") +
  
  ## errors
  annotate("text", x = 4.17, y = 2,
           label = expression(italic(e) * phantom("")[M]),
           size = 4, family = "Times New Roman",
           angle = 0)+
  
  annotate("text", x = 7.07, y = 0,
           label = expression(italic(e) * phantom("")[Y]),
           size = 4, family = "Times New Roman",
           angle = 0)+
  
  
  annotate("text", x = 3.1, y = 3.1, label = "Statistical Model",
           family = "Times New Roman", fontface = "bold", size = 6) +
  
  coord_equal(xlim = c(-0.5, 7.5), ylim = c(-0.5, 3.5), expand = TRUE) +
  theme_void()
p_model


ggsave("model.png", p_model,
       width = 12, height = 9, dpi = 300)




